package com.hqu.ms.person;

import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.actionbarsherlock.app.SherlockListFragment;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.ViewById;
import com.hqu.ms.R;
import com.hqu.ms.db.FavDB;
import com.hqu.ms.db.FavDBDao;
import com.hqu.ms.db.MSDB;
import com.hqu.ms.db.MSFav;
import com.hqu.ms.detail.DetailActivity_;
import com.hqu.ms.entities.StoreEntivity;


@EFragment(R.layout.ms_person_fragment)
public class PersonFragment extends SherlockListFragment implements OnItemClickListener{
	
	private FavListAdapter mAdapter ;
	private FavDBDao mDao;
	@ViewById
	CheckBox cbEdit;
	
	@AfterViews
	void afterViewInjected(){
		cbEdit.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				mAdapter.setShowDelete(isChecked);
				mAdapter.notifyDataSetChanged();
			}
		});
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		initDB();
	}
	
	private Animation mSlideOut = null;
	
	private void initListView() {
		mSlideOut = AnimationUtils.loadAnimation(getActivity(), R.anim.base_slide_right_out);
		mSlideOut.setFillAfter(true);
		
		mAdapter = new FavListAdapter(getActivity(), null, mDao, this);
		getListView().setAdapter(mAdapter);
		getListView().setOnItemClickListener(this);
	}
	
	void delete(FavDB item) {
		mDao.queryBuilder()
			.where(FavDBDao.Properties.Mg_id.eq(item.getMg_id()))
			.buildDelete()
			.executeDeleteWithoutDetachingEntities();
		updateListView();
	}

	public void updateListView(){

		Cursor cursor = MSFav.cursorQueryAll(mDao);
		if (cursor == null || cursor.getCount() == 0 ) {
			cbEdit.setChecked(false);
			cbEdit.setEnabled(false);
		} else {
			cbEdit.setEnabled(true);
		}
		mAdapter.changeCursor(cursor);
		mAdapter.notifyDataSetChanged();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.ms_person_fragment, null);
	}
	void initDB() {
		mDao = MSDB.newSession().getFavDBDao();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		initListView();
		
		updateListView();
		mAdapter.setShowDelete(cbEdit.isChecked());
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(cbEdit.isChecked()){
			delete((FavDB)view.getTag());
		}else{
			StoreEntivity store = new StoreEntivity();
			FavDB favDB = (FavDB) view.getTag();
			store.setMg_id(favDB.getMg_id());
			store.setMg_name(favDB.getMg_name());
			store.setMg_address(favDB.getMg_address());
			store.setMg_phone(favDB.getMg_phone());
			store.setMg_introduce(favDB.getMg_introduce());
			store.setMg_picture(favDB.getMg_picture());
			
			DetailActivity_.intent(getActivity()).store(store).start();
			getActivity().overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_left_out);
		}
	}
	
	
}
